﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.DataAccess.Common;
using TVGuide.DataAccess.Exceptions;

namespace TVGuide.DataAccess.Repository
{
    class HProgramRepository
    {
        #region Singleton pattern

        private static tvchanneldbEntities model;
        private static HProgramRepository instance;

        private HProgramRepository() { model = new tvchanneldbEntities(); }

        public static HProgramRepository Instance()
        {
            if (instance == null)
                instance = new HProgramRepository();
            return instance;
        }

        #endregion

        #region Public Methods
        
        void add(program item)
        {
            add(item, true);
        }

        void add(program item, bool commitChange)
        {
            if (item == null)
                throw new InvalidArgsException("The parameter is null");

            model.hprograms.AddObject(tohprogram(item));

            if (commitChange)
                model.SaveChanges();
        }

        void addRange(IEnumerable<program> items)
        {
            try
            {
                foreach (var item in items)
                {
                    add(item, false);
                }
            }
            catch (InvalidArgsException e)
            {
                // TODO rollback changes.
                InvalidArgsException ex = new InvalidArgsException(e.Message + "\nNo items were committed.");
                throw ex;
            }
            model.SaveChanges();
        }

        hprogram get(int id)
        {
            throw new NotImplementedException();
        }

        IEnumerable<HProgram> get(Predicate<HProgram> func)
        {
            throw new NotImplementedException();
        }

        IEnumerable<HProgram> getAllItems()
        {
            throw new NotImplementedException();
        }

        //public void remove(int id)
        //{
        //    throw new NotImplementedException();
        //}

        void remove(Predicate<hprogram> func)
        {
            List<hprogram> removeList = new List<hprogram>();
            foreach (var item in model.hprograms)
            {
                if (func(item))
                    removeList.Add(item);
            }

            foreach (var item in removeList)
            {
                model.hprograms.DeleteObject(item);
            }
            model.SaveChanges();
        }

        void removeAll()
        {
            remove(o => true);
        }

        #endregion

        private hprogram tohprogram(program p)
        {
            if (p == null)
                return null;
            hprogram prog = new hprogram();
            prog.categoryID = p.categoryID;
            prog.channelID = p.channelID;
            prog.countryID = p.countryID;
            prog.description = p.description;
            prog.languageID = p.languageID;
            prog.title = p.title;
            prog.date = p.date;
            prog.hisDate = DateTime.Now;
            return prog;
        }
    }
}
